export default class FlowChartDataHandle {

  static getIpNum(ipAddress) {/*获取IP数*/

    let ip = ipAddress.split('.')

    let a = parseInt(ip[0])

    let b = parseInt(ip[1])

    let c = parseInt(ip[2])

    let d = parseInt(ip[3])

    var ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d

    return ipNum

  }


  static isInner(userIp, begin, end) {

    return (userIp >= begin) && (userIp <= end)

  }

  static IsInnerIP(ipAddress) {

    let isInnerIp = false//默认给定IP不是内网IP
    let ipNum = this.getIpNum(ipAddress)

    /**

     * 私有IP：A类  10.0.0.0    -10.255.255.255

     *       B类  172.16.0.0  -172.31.255.255

     *       C类  192.168.0.0 -192.168.255.255

     *       D类   127.0.0.0   -127.255.255.255(环回地址)

     **/

    let aBegin = this.getIpNum('10.0.0.0')

    let aEnd = this.getIpNum('10.255.255.255')

    let bBegin = this.getIpNum('172.16.0.0')

    let bEnd = this.getIpNum('172.31.255.255')

    let cBegin = this.getIpNum('192.168.0.0')

    let cEnd = this.getIpNum('192.168.255.255')

    let dBegin = this.getIpNum('127.0.0.0')

    let dEnd = this.getIpNum('127.255.255.255')

    isInnerIp = this.isInner(ipNum, aBegin, aEnd) || this.isInner(ipNum, bBegin, bEnd) || this.isInner(ipNum, cBegin, cEnd) || this.isInner(ipNum, dBegin, dEnd)

    //console.log('是否是内网:'+isInnerIp);

    return isInnerIp
  }

//找大类型与小类型
  static findType(typeJson, typeId) {
    let resType = []
    //console.log(typeJson);
    // console.log(typeId);
    for (let i = 0; i < typeJson.accessType.length; i++) {
      //console.log(typeId);
      //console.log(typeJson.accessType[i].id);
      if (typeJson.accessType[i].id === typeId) {
        resType.push(typeJson.accessType[i].typeName)
        resType.push(typeJson.accessType[i].siteName)
      }
    }
    //console.log(resType)
    return resType
  }

  static sortData(dataList) {
    //console.log(dataList)
    for (let i = 0; i < dataList.length; i++) {
      let dataItem = dataList[i]
      for (var k in dataItem) {
        if (k === 'data') {
          for (let j = 0; j < dataItem[k].length; j++) {
            if (dataItem[k][j] === undefined) {
              dataItem[k][j] = 0
            }
          }
        }
      }
    }
    return null
  }

//在数据列表中找到指定名称类型的数据变量
  static findDataItem(dataList, typeName, flowDir) {
    let flag = false
    let flag1 = false
    //console.log(dataList)
    for (let i = 0; i < dataList.length; i++) {
      flag = false
      flag1 = false
      let dataItem = dataList[i]

      for (var k in dataItem) {
        // console.log(k)
        // console.log(dataItem[k])
        if (k === 'name' && dataItem[k] === typeName) {
          flag = true
        }
        // 判断是否是上下行流量
        if (k === 'stack' && dataItem[k] === flowDir) {
          flag1 = true
        }

        if (flag && flag1 && k === 'data') {
          return dataItem[k]
        }
      }
    }
    return null
  }

  static daysBetween(DateOne, DateTwo) {
    let OneMonth = DateOne.substring(5, DateOne.lastIndexOf('-'))
    let OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf('-') + 1)
    let OneYear = DateOne.substring(0, DateOne.indexOf('-'))
    let TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf('-'))
    let TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf('-') + 1)
    let TwoYear = DateTwo.substring(0, DateTwo.indexOf('-'))
    let cha = ((Date.parse(OneMonth + '/' + OneDay + '/' + OneYear) - Date.parse(TwoMonth + '/' + TwoDay + '/' + TwoYear)) / 86400000)

    return Math.abs(cha)
  }

  static dateTran(myDate) {
    let dateString
    let temp = myDate
    dateString = temp.getFullYear()
    dateString += '-'
    dateString += temp.getMonth() + 1
    dateString += '-'
    dateString += temp.getDate()

    return dateString
  }

  static getDateInter(date0, date1) {
    if (date0 > date1) {
      return Math.floor(((date0 -
        date1) / 60 / 60 / 24 / 1000))
    } else {
      return Math.floor(((date1 -
        date0) / 60 / 60 / 24 / 1000))
    }
  }

  static saveAccessTypeList(accessTypeList, accessTypeName) {
    for (let i = 0; i < accessTypeList.length; i++) {
      if (accessTypeList[i] === accessTypeName) {
        return
      }
    }
    accessTypeList.push(accessTypeName)
  }

  //找到表中的协议类型
  static findPcoItem(dataPcoList, poctolName) {
    let flag = false
    for (let i = 0; i < dataPcoList.length; i++) {
      let tempItem = dataPcoList[i]
      flag = false
      for (var key in tempItem) {
        var value = tempItem[key]
        if (key === 'name' && value === poctolName) {
          flag = true
        } else if (key === 'value' && flag) {
          return tempItem
        }
      }
    }
    return null
  }
  static protocolTran(protocolID){
      switch (protocolID) {
        case 1:
          return 'ICMP'
          break;
        case  2:
          return 'IGMP'
          break;
        case 6:
          return 'TCP'
          break;
        case 8:
          return 'EGP'
          break;
        case 9:
          return 'IGP'
          break;
        case 17:
          return 'UDP'
          break;
        case 41:
          return 'IPv6'
          break;
        case 89:
          return 'OSPF'
          break;
      }
      return 'undefined'
  }



  static tran(jsonVal, typeJson, startDate, showType) {
    let resDataList = []
    let capPack = jsonVal.userCapPackClassify
    // if (capPack === undefined || capPack === null) {
    //   capPack = jsonVal.userCapPackClassifyTran
    // }

    //////线段图
    let accessType
    //数据列表
    let dataList = []
    ///////

    /////协议饼图////////////////////
    //数据列表
    let dataPcoList = []
    let poctolList = []
    /////////////////////////////////////

    ////折线图的数据处理////////////////////////////////
    //数据列表
    let accessTypeList = []
    let dataLineList = []
    let dataLineItemHead = ['product']
    //let dataItem=[];
    dataLineList.push(dataLineItemHead)
    //默认显示大类
    if (showType === undefined) {
      showType = 0
    }
    ///////////////////////////////////////////////////


    //数据项
    /*let dataItem={
    'name':'',
    'type':'bar',
    'statck':'',
    'data':[]
    };
    */
    //console.log(startDate);
    if (capPack.length === 0) {
      //线段图
      resDataList.push(dataList)
      //曲线图
      resDataList.push(dataLineList)
      //线段图类型列表
      resDataList.push(accessTypeList)

      //协议饼图
      resDataList.push(dataPcoList)
      //协议饼图类型列表
      resDataList.push(poctolList)
      return resDataList
    }
    for (let i = 0; i < capPack.length; i++) {
      //////////////////////////////////线段图/////////////////////////////////////////////
      let dataItemFind
      //找到对应的类型名称W
      accessType = this.findType(typeJson, capPack[i].accessTypeId)
      // 保存访问类型
      this.saveAccessTypeList(accessTypeList, accessType[0])

      //console.log(accessType)
      //是否内网,或者是否上传
      let isInner = this.IsInnerIP(capPack[i].srcAddr)
      //找到指定类型的数据变量
      dataItemFind = this.findDataItem(dataList, accessType[0], isInner ? '上行流量' : '下行流量')

      if (dataItemFind === null) {
        dataItemFind = []
        let currentDate = new Date(this.dateTran(new Date(capPack[i].date)))
        let index = this.getDateInter(currentDate, startDate)
        dataItemFind[index] = capPack[i].packSize
        dataList.push({
          'name': accessType[0],
          'type': 'bar',
          'stack': isInner ? '上行流量' : '下行流量',
          'data': dataItemFind
        })
      } else {
        let currentDate = new Date(this.dateTran(new Date(capPack[i].date)))
        let index = this.getDateInter(currentDate, startDate)
        console.log(currentDate)
        console.log(startDate)
        console.log(index)

        if (dataItemFind[index] != null) {
          dataItemFind[index] += capPack[i].packSize
        } else {
          // console.log(index);
          dataItemFind[index] = capPack[i].packSize
        }
      }
      /////////////////////////////////////////////////////////////////////////////////////////////

      /////////////////////////////////////折线图//////////////////////////////////////////////////
      let dataItemFindLineChart = []

      //找到对应的类型名称W
      //accessType=this.findType(typeJson,capPack[i].accessTypeId);

      dataItemFindLineChart = this.findLineChartDataItem(dataLineList, accessType[showType])

      if (dataItemFindLineChart === null) {
        dataItemFindLineChart = []
        //如果没有，则还要在前面插入零
        dataItemFindLineChart.push(accessType[showType])
        for (let j = 0; j < i + 1; j++) {
          dataItemFindLineChart.push(0)
        }

        dataLineList.push(dataItemFindLineChart)
      } else {
        dataItemFindLineChart.push(capPack[i].packSize)
      }

      this.addOtherItem(dataLineList, dataItemFindLineChart)
      //capPack[i].date
      dataLineItemHead.push(capPack[i].date)

      ////////////////////////////////////////////////////////////////////////////////////////////

      ///////////////////////////////////协议饼图//////////////////////////////////////
      //let dataItemFind;
      dataItemFind = this.findPcoItem(dataPcoList, this.protocolTran(parseInt(capPack[i].protcolType)))

      if (dataItemFind === null) {
        dataItemFind = {
          name:  this.protocolTran(parseInt(capPack[i].protcolType)),
          value: capPack[i].packSize
        }
        dataPcoList.push(dataItemFind)
      } else {
        dataItemFind.value += capPack[i].packSize
      }

    }

    for (let i = 0; i < dataPcoList.length; i++) {
      poctolList.push(dataPcoList[i].name)
    }

    this.sortData(dataList)
    //线段图
    resDataList.push(dataList)
    //曲线图
    resDataList.push(dataLineList)
    //线段图类型列表
    resDataList.push(accessTypeList)

    //协议饼图
    resDataList.push(dataPcoList)
    //协议饼图类型列表
    resDataList.push(poctolList)

    return resDataList

  }


  //除指定项其它项都增加数据0
  static addOtherItem(dataList, noAddItem) {
    for (let i = 1; i < dataList.length; i++) {
      if (dataList[i] !== noAddItem) {
        dataList[i].push(0)
      }
    }
  }

  //找到需要的曲线图数据项
  static findLineChartDataItem(dataList, TypeName) {
    for (let i = 0; i < dataList.length; i++) {
      if (dataList[i][0] != null) {
        if (dataList[i][0] === TypeName) {
          return dataList[i]
        }
      }
    }
    return null
  }

///没有用到
  static lineChart(jsonVal, typeJson, showType) {

    let capPack = jsonVal.userCapPackClassify
    let accessType

    //数据列表
    let dataList = []
    let dataItemHead = ['product']
    //let dataItem=[];
    dataList.push(dataItemHead)

    for (let i = 0; i < capPack.length; i++) {

      let dataItemFind = []

      //找到对应的类型名称W
      accessType = this.findType(typeJson, capPack[i].accessTypeId)

      dataItemFind = this.findLineChartDataItem(dataList, accessType[showType])

      if (dataItemFind === null) {
        dataItemFind = []
        //如果没有，则还要在前面插入零
        dataItemFind.push(accessType[showType])
        for (let j = 0; j < i + 1; j++) {
          dataItemFind.push(0)
        }

        dataList.push(dataItemFind)
      } else {
        dataItemFind.push(capPack[i].packSize)
      }

      this.addOtherItem(dataList, dataItemFind)
      //capPack[i].date
      dataItemHead.push(capPack[i].date)
    }

    return dataList
  }

  static saveAlarmType(alarmTypelist, saveTypeName) {
    for (let i = 0; i < alarmTypelist.length; i++) {
      if (alarmTypelist[i] === saveTypeName) {
        return
      }
    }
    alarmTypelist.push(saveTypeName)
  }

  //警告信息转换
  static alarmTran(alarmJson, startDate) {
    let dayCount = 0
    let resDataList = []//返回的数据
    let dayStrList = []//显示的天
    let alarmTypeNameList = []//报警的类型
    let listAlarmDate = []//存放数据

    let alarmData = alarmJson.userAlarmList

    resDataList.push(listAlarmDate)
    resDataList.push(alarmTypeNameList)
    resDataList.push(dayStrList)


    for (let i = 0; i < alarmData.length; i++) {
      let alarmDataRes = []
      this.saveAlarmType(alarmTypeNameList, alarmData[i].alarmType)
      alarmDataRes = this.findDataItem(listAlarmDate, alarmData[i].alarmType, 'a')
      if (alarmDataRes == null) {
        alarmDataRes = []

        alarmDataRes.push(1)
        listAlarmDate.push({
          'name': alarmData[i].alarmType,
          'type': 'bar',
          'stack': 'a',
          'coordinateSystem': 'polar',
          'data': alarmDataRes
        })
      } else {
        let index = this.getDateInter(startDate, new Date(alarmData[i].date))
        //console.log(index);
        if (alarmDataRes[index] !== undefined) {
          alarmDataRes[index] += 1
        } else {
          alarmDataRes[index] = 1
        }
        if (index > dayCount) {
          dayCount = index
        }
      }

    }
    for (let i = 0; i < dayCount + 1; i++) {
      dayStrList.push('第' + (i + 1) + '天')
    }
    this.sortData(listAlarmDate)

    return resDataList
  }

  //
  // static findPcoItem(dataPcoList,poctolName){
  //   let flag=false;
  //   for(let i=0;i<dataPcoList.length;i++){
  //     let tempItem=dataPcoList[i];
  //     flag=false;
  //     for (var key in tempItem) {
  //       var value=tempItem[key];
  //       if(key==='name'&&value===poctolName){
  //         flag=true;
  //       }else if(key==='value'&&flag){
  //         return tempItem;
  //       }
  //     }
  //   }
  //   return null;
  // }

  //用户告警信息饼图
  static userAlarmTran(jsonVal) {
    //数据列表
    let dataPcoList = []
    let poctolList = []
    let capPack = jsonVal.userAlarmList
    //{name: "udp", value: 500}

    for (let i = 0; i < capPack.length; i++) {
      let dataItemFind
      dataItemFind = this.findPcoItem(dataPcoList, capPack[i].alarmType)

      if (dataItemFind === null) {
        dataItemFind = {
          name: capPack[i].alarmType,
          value: 1
        }
        dataPcoList.push(dataItemFind)
      } else {
        dataItemFind.value++
      }
    }
    for (let i = 0; i < dataPcoList.length; i++) {
      poctolList.push(dataPcoList[i].name)
    }
    return [dataPcoList, poctolList]
  }

  //用户喜好分类饼图
  static userAiHaoFenLeiTran(jsonVal, typeJson, showType) {
    //数据列表
    let dataPcoList = []
    let poctolList = []
    let capPack = jsonVal.userCapPackClassify
    let accessType

    for (let i = 0; i < capPack.length; i++) {
      let dataItemFind
      accessType = this.findType(typeJson, capPack[i].accessTypeId)

      dataItemFind = this.findPcoItem(dataPcoList, accessType[showType])

      if (dataItemFind === null) {
        dataItemFind = {
          name: accessType[showType],
          value: capPack[i].packSize
        }
        dataPcoList.push(dataItemFind)
      } else {
        dataItemFind.value += capPack[i].packSize
      }
    }
    for (let i = 0; i < dataPcoList.length; i++) {
      poctolList.push(dataPcoList[i].name)
    }

    return [dataPcoList, poctolList]
  }

  //流量曲线图
  static FlowLineChart(jsonVal) {
    let capPack = jsonVal.userFlowInfo
    let accessType
    //数据列表
    let dataList = []
    let dataItemHead = ['product']
    //let dataItem=[];
    dataList.push(dataItemHead)

    let lastVal = 0

    for (let i = 0; i < capPack.length; i++) {
      let dataItemFind = null

      //找到对应的类型名称W
      //accessType=findType(typeJson,capPack[i].accessTypeId);

      dataItemFind = this.findLineChartDataItem(dataList, capPack[i].addr)

      if (dataItemFind === null) {
        dataItemFind = []
        //如果没有，则还要在前面插入零
        dataItemFind.push(capPack[i].addr)
        for (let j = 0; j < i + 1; j++) {
          dataItemFind.push(0)
        }

        dataList.push(dataItemFind)
      } else {
        let temp_val
        temp_val = capPack[i].downFlowSize + capPack[i].upFlowSize - lastVal
        if (temp_val > 0) {
          dataItemFind.push(temp_val)
          lastVal = capPack[i].downFlowSize + capPack[i].upFlowSize
        } else {
          lastVal = 0
        }

      }
      this.addOtherItem(dataList, dataItemFind)
      //capPack[i].date
      dataItemHead.push(capPack[i].date)

      dataItemFind = this.findLineChartDataItem(dataList, capPack[i].addr + 'US')
      if (dataItemFind === null) {
        dataItemFind = []
        //如果没有，则还要在前面插入零
        dataItemFind.push(capPack[i].addr + 'US')
        for (let j = 0; j < i + 1; j++) {
          dataItemFind.push(0)
        }

        dataList.push(dataItemFind)
      } else {
        dataItemFind.push(capPack[i].upFlowSpeed)
      }
      this.addOtherItem(dataList, dataItemFind)
      dataItemHead.push(capPack[i].date)

      dataItemFind = this.findLineChartDataItem(dataList, capPack[i].addr + 'DS')
      if (dataItemFind === null) {
        dataItemFind = []
        //如果没有，则还要在前面插入零
        dataItemFind.push(capPack[i].addr + 'DS')
        for (let j = 0; j < i + 1; j++) {
          dataItemFind.push(0)
        }

        dataList.push(dataItemFind)
      } else {
        dataItemFind.push(capPack[i].downFlowSpeed)
      }
      this.addOtherItem(dataList, dataItemFind)
      dataItemHead.push(capPack[i].date)
    }

    return dataList
  }

  //速度曲线图
  static SpeedLineChart(jsonVal) {

    let capPack = jsonVal.userFlowInfo
    let accessType

    //数据列表
    let dataList = []
    let dataItemHead = ['product']
    //let dataItem=[];
    dataList.push(dataItemHead)

    let lastVal = 0

    for (let i = 0; i < capPack.length; i++) {
      let dataItemFind = null

      dataItemFind = this.findLineChartDataItem(dataList, capPack[i].addr + 'US')
      if (dataItemFind === null) {
        dataItemFind = []
        //如果没有，则还要在前面插入零
        dataItemFind.push(capPack[i].addr + 'US')
        for (let j = 0; j < i + 1; j++) {
          dataItemFind.push(0)
        }

        dataList.push(dataItemFind)
      } else {
        dataItemFind.push(capPack[i].upFlowSpeed)
      }
      this.addOtherItem(dataList, dataItemFind)
      dataItemHead.push(capPack[i].date)

      dataItemFind = this.findLineChartDataItem(dataList, capPack[i].addr + 'DS')
      if (dataItemFind === null) {
        dataItemFind = []
        //如果没有，则还要在前面插入零
        dataItemFind.push(capPack[i].addr + 'DS')
        for (let j = 0; j < i + 1; j++) {
          dataItemFind.push(0)
        }
        dataList.push(dataItemFind)
      } else {
        dataItemFind.push(capPack[i].downFlowSpeed)
      }
      this.addOtherItem(dataList, dataItemFind)
      dataItemHead.push(capPack[i].date)
    }
    return dataList
  }


  static typeJson = {
    'status': 200,
    'totalFlow': 0,
    'accessType': [{
      'id': 1,
      'typeName': '音乐',
      'ip': '118.112.11.101',
      'port': 443,
      'siteName': 'QQ音乐'
    }, {
      'id': 2,
      'typeName': '音乐',
      'ip': '110.185.115.204',
      'port': 443,
      'siteName': 'QQ音乐'
    }, {
      'id': 3,
      'typeName': '音乐',
      'ip': '118.112.22.184',
      'port': 443,
      'siteName': 'QQ音乐'
    }, {
      'id': 12,
      'typeName': '政治',
      'ip': '110.185.125.140',
      'port': 443,
      'siteName': '学习强国'
    }, {
      'id': 13,
      'typeName': '黑名单',
      'ip': '125.66.191.151',
      'port': 443,
      'siteName': '4399小游戏'
    }, {
      'id': 14,
      'typeName': '黑名单',
      'ip': '125.66.191.151',
      'port': 80,
      'siteName': '4399小游戏'
    }, {
      'id': 15,
      'typeName': '其它',
      'ip': '0.0.0.0',
      'port': -1,
      'siteName': '其它'
    }, {
      'id': 18,
      'typeName': '视频',
      'ip': '211.97.167.31',
      'port': -1,
      'siteName': '腾讯视频'
    }, {
      'id': 21,
      'typeName': '视频',
      'ip': '122.136.198.26',
      'port': -1,
      'siteName': '腾讯视频'
    }, {
      'id': 22,
      'typeName': '视频',
      'ip': '123.120.66.157',
      'port': -1,
      'siteName': '腾讯视频'
    }, {
      'id': 23,
      'typeName': '政治',
      'ip': '110.185.125.187',
      'port': 443,
      'siteName': '学习强国'
    }, {
      'id': 24,
      'typeName': '政治',
      'ip': '110.185.125.141',
      'port': 443,
      'siteName': '学习强国'
    }, {
      'id': 25,
      'typeName': '音乐',
      'ip': '101.72.201.35',
      'port': 443,
      'siteName': '网易云音乐'
    }, {
      'id': 26,
      'typeName': '视频',
      'ip': '223.221.180.20',
      'port': 443,
      'siteName': '腾讯视频'
    }, {
      'id': 27,
      'typeName': '视频',
      'ip': '101.206.186.14',
      'port': 443,
      'siteName': '腾讯视频'
    }, {
      'id': 28,
      'typeName': '视频',
      'ip': '36.250.239.37',
      'port': 443,
      'siteName': '爱奇艺'
    }, {
      'id': 29,
      'typeName': '视频',
      'ip': '1.82.215.22',
      'port': 443,
      'siteName': '腾讯视频'
    }, {
      'id': 30,
      'typeName': '视频',
      'ip': '14.215.178.14',
      'port': 443,
      'siteName': '爱奇艺'
    }, {
      'id': 31,
      'typeName': '视频',
      'ip': '112.80.255.84',
      'port': 443,
      'siteName': '爱奇艺'
    }, {
      'id': 32,
      'typeName': '视频',
      'ip': '180.97.93.50',
      'port': 443,
      'siteName': '爱奇艺'
    }, {
      'id': 33,
      'typeName': '黑名单',
      'ip': '180.97.93.50',
      'port': 443,
      'siteName': '4399'

    }]
  }
}


// console.log(jsonAccessType);
// console.log(typeJson);
// tran(jsonAccessType,typeJson);



